home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / os2 / pccts.zip / DLGAUTO.H < prev    next >
C/C++ Source or Header  |  1992-12-08  |  8KB  |  362 lines

  1. /* dlgauto.h automaton
  2.  *
  3.  * SOFTWARE RIGHTS
  4.  *
  5.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  6.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  7.  * company may do whatever they wish with source code distributed with
  8.  * PCCTS or the code generated by PCCTS, including the incorporation of
  9.  * PCCTS, or its output, into commerical software.
  10.  * 
  11.  * We encourage users to develop software with PCCTS.  However, we do ask
  12.  * that credit is given to us for developing PCCTS.  By "credit",
  13.  * we mean that if you incorporate our source code into one of your
  14.  * programs (commercial product, research project, or otherwise) that you
  15.  * acknowledge this fact somewhere in the documentation, research report,
  16.  * etc...  If you like PCCTS and have developed a nice tool with the
  17.  * output, please mention that you developed it using PCCTS.  In
  18.  * addition, we ask that this header remain intact in our source code.
  19.  * As long as these guidelines are kept, we expect to continue enhancing
  20.  * this system and expect to make other tools available as they are
  21.  * completed.
  22.  *
  23.  * ANTLR 1.06
  24.  * Will Cohen
  25.  * Purdue University
  26.  * 1989-1992
  27.  */
  28.  
  29. char    *zzlextext;    /* text of most recently matched token */
  30. char    *zzbegexpr;    /* beginning of last reg expr recogn. */
  31. char    *zzendexpr;    /* beginning of last reg expr recogn. */
  32. int    zzbufsize;    /* number of characters in zzlextext */
  33. int    zzbegcol = 0;    /* column that first character of token is in*/
  34. int    zzendcol = 0;    /* column that last character of token is in */
  35. int    zzline = 1;    /* line current token is on */
  36. int    zzchar;        /* character to determine next state */
  37. int    zzbufovf;    /* indicates that buffer too small for text */
  38. int    zzcharfull = 0;
  39. static char    *zznextpos;/* points to next available position in zzlextext*/
  40. static int     zzclass;
  41.  
  42. #ifdef __STDC__
  43. void    zzerrstd(char *);
  44. void    (*zzerr)(char *)=zzerrstd;/* pointer to error reporting function */
  45. #else
  46. void    zzerrstd();
  47. void    (*zzerr)()=zzerrstd;    /* pointer to error reporting function */
  48. #endif
  49.  
  50. static FILE    *zzstream_in;
  51. extern int    zzerr_in();
  52. static int    (*zzfunc_in)() = zzerr_in;
  53.  
  54. static int     zzauto = 0;
  55. static int    zzadd_erase;
  56. static char     zzebuf[70];
  57.  
  58. #ifdef ZZCOL
  59. #define ZZINC (++zzendcol)
  60. #else
  61. #define ZZINC
  62. #endif
  63.  
  64.  
  65. #define ZZGETC_STREAM {zzchar = getc(zzstream_in); zzclass = ZZSHIFT(zzchar);}
  66.  
  67. #define ZZGETC_FUNC {zzchar = (*zzfunc_in)(); zzclass = ZZSHIFT(zzchar);}
  68.  
  69. void
  70. #ifdef __STDC__
  71. zzrdstream( FILE *f )
  72. #else
  73. zzrdstream( f )
  74. FILE *f;
  75. #endif
  76. {
  77.     /* make sure that it is really set to something, otherwise just 
  78.        leave it be.
  79.     */
  80.     if (f){
  81.         /* make sure that there is always someplace to get input
  82.            before closing zzstream_in
  83.         */
  84.         if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
  85.         zzline = 1;
  86.         zzstream_in = f;
  87.         zzfunc_in = NULL;
  88.         zzcharfull = 0;
  89.     }
  90. }
  91.  
  92. void
  93. #ifdef __STDC__
  94. zzrdfunc( int (*f)() )
  95. #else
  96. zzrdfunc( f )
  97. int (*f)();
  98. #endif
  99. {
  100.     /* make sure that it is really set to something, otherwise just 
  101.        leave it be.
  102.     */
  103.     if (f){
  104.         /* make sure that there is always someplace to get input
  105.            before closing zzstream_in
  106.         */
  107.         if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
  108.         zzline = 1;
  109.         zzstream_in = NULL;
  110.         zzfunc_in = f;
  111.         zzcharfull = 0;
  112.     }
  113. }
  114.  
  115.  
  116. void
  117. zzclose_stream()
  118. {
  119. #if 0
  120.     fclose( zzstream_in );
  121.     zzstream_in = NULL;
  122.     zzfunc_in = NULL;
  123. #endif
  124. }
  125.  
  126. void
  127. #ifdef __STDC__
  128. zzmode( int m )
  129. #else
  130. zzmode( m )
  131. int m;
  132. #endif
  133. {
  134.     /* points to base of dfa table */
  135.     if (m<MAX_MODE){
  136.         zzauto = m;
  137.         /* have to redo class since using different compression */
  138.         zzclass = ZZSHIFT(zzchar);
  139.     }else{
  140.         sprintf(zzebuf,"Invalid automaton mode = %d ",m);
  141.         zzerr(zzebuf);
  142.     }
  143. }
  144.  
  145. /* erase what is currently in the buffer, and get a new reg. expr */
  146. void
  147. zzskip()
  148. {
  149.     zzadd_erase = 1;
  150. }
  151.  
  152. /* don't erase what is in the zzlextext buffer, add on to it */
  153. void
  154. zzmore()
  155. {
  156.     zzadd_erase = 2;
  157. }
  158.  
  159. /* substitute c for the reg. expr last matched and is in the buffer */
  160. #ifdef __STDC__
  161. void
  162. zzreplchar(char c)
  163. #else
  164. void
  165. zzreplchar(c)
  166. char c;
  167. #endif
  168. {
  169.     /* can't allow overwriting null at end of string */
  170.     if (zzbegexpr < &zzlextext[zzbufsize-1]){
  171.         *zzbegexpr = c;
  172.         *(zzbegexpr+1) = '\0';
  173.     }
  174.     zzendexpr = zzbegexpr;
  175.     zznextpos = zzbegexpr + 1;
  176. }
  177.  
  178. /* replace the string s for the reg. expr last matched and in the buffer */
  179. void
  180. #ifdef __STDC__
  181. zzreplstr(register char *s)
  182. #else
  183. zzreplstr(s)
  184. register char *s;
  185. #endif
  186. {
  187.     register char *l= &zzlextext[zzbufsize -1];
  188.  
  189.     zznextpos = zzbegexpr;
  190.     if (s){
  191.          while ((zznextpos <= l) && (*(zznextpos++) = *(s++))){
  192.             /* empty */
  193.         }
  194.         /* correct for NULL at end of string */
  195.         zznextpos--;
  196.     }
  197.     if ((zznextpos <= l) && (*(--s) == 0)){
  198.         zzbufovf = 0;
  199.     }else{
  200.         zzbufovf = 1;
  201.     }
  202.     *(zznextpos) = '\0';
  203.     zzendexpr = zznextpos - 1;
  204. }
  205.  
  206. void
  207. zzgettok()
  208. {
  209.     register int state, newstate;
  210.     /* last space reserved for the null char */
  211.     register char *lastpos;
  212.  
  213. skip:
  214.     zzbufovf = 0;
  215.     lastpos = &zzlextext[zzbufsize-1];
  216.     zznextpos = zzlextext;
  217.     zzbegcol = zzendcol+1;
  218. more:
  219.     zzbegexpr = zznextpos;
  220. #ifdef ZZINTERACTIVE
  221.     /* interactive version of automaton */
  222.     /* if there is something in zzchar, process it */
  223.     state = newstate = dfa_base[zzauto];
  224.     if (zzcharfull){
  225.         ZZINC;
  226.         /* Truncate matching buffer to size (not an error) */
  227.         if (zznextpos < lastpos){
  228.             *(zznextpos++) = zzchar;
  229.         }else{
  230.             zzbufovf = 1;
  231.         }
  232.         newstate = dfa[state][zzclass];
  233.     }
  234.     if (zzstream_in)
  235.         while (zzalternatives[newstate]){
  236.             state = newstate;
  237.             ZZGETC_STREAM;
  238.             ZZINC;
  239.             /* Truncate matching buffer to size (not an error) */
  240.             if (zznextpos < lastpos){
  241.                 *(zznextpos++) = zzchar;
  242.             }else{
  243.                 zzbufovf = 1;
  244.             }
  245.             newstate = dfa[state][zzclass];
  246.         }
  247.     else if (zzfunc_in)
  248.         while (zzalternatives[newstate]){
  249.             state = newstate;
  250.             ZZGETC_STREAM;
  251.             ZZINC;
  252.             /* Truncate matching buffer to size (not an error) */
  253.             if (zznextpos < lastpos){
  254.                 *(zznextpos++) = zzchar;
  255.             }else{
  256.                 zzbufovf = 1;
  257.             }
  258.             newstate = dfa[state][zzclass];
  259.         }
  260.     /* figure out if last character really part of token */
  261.     if ((state != dfa_base[zzauto]) && (newstate == DfaStates)){
  262.         zzcharfull = 1;
  263.         --zznextpos;
  264.     }else{
  265.         zzcharfull = 0;
  266.         state = newstate;
  267.     }
  268.     *(zznextpos) = '\0';
  269.     /* Able to transition out of start state to some non err state?*/
  270.     if ( state == dfa_base[zzauto] ){
  271.         /* make sure doesn't get stuck */
  272.         zzadvance();
  273.     }
  274. #else
  275.     /* non-interactive version of automaton */
  276.     if (!zzcharfull)
  277.         zzadvance();
  278.     else
  279.         ZZINC;
  280.     state = dfa_base[zzauto];
  281.     if (zzstream_in)
  282.         while ((newstate = dfa[state][zzclass]) != DfaStates){
  283.             state = newstate;
  284.             /* Truncate matching buffer to size (not an error) */
  285.             if (zznextpos < lastpos){
  286.                 *(zznextpos++) = zzchar;
  287.             }else{
  288.                 zzbufovf = 1;
  289.             }
  290.             ZZGETC_STREAM;
  291.             ZZINC;
  292.         }
  293.     else if (zzfunc_in)
  294.         while ((newstate = dfa[state][zzclass]) != DfaStates){
  295.             state = newstate;
  296.             /* Truncate matching buffer to size (not an error) */
  297.             if (zznextpos < lastpos){
  298.                 *(zznextpos++) = zzchar;
  299.             }else{
  300.                 zzbufovf = 1;
  301.             }
  302.             ZZGETC_FUNC;
  303.             ZZINC;
  304.         }
  305.     zzcharfull = 1;
  306.     if ( state == dfa_base[zzauto] ){
  307.         if (zznextpos < lastpos){
  308.             *(zznextpos++) = zzchar;
  309.         }else{
  310.             zzbufovf = 1;
  311.         }
  312.         *zznextpos = '\0';
  313.         /* make sure doesn't get stuck */
  314.         zzadvance();
  315.     }else{
  316.         *zznextpos = '\0';
  317.     }
  318. #endif
  319. #ifdef ZZCOL
  320.     zzendcol -= zzcharfull;
  321. #endif
  322.     zzendexpr = zznextpos -1;
  323.     zzadd_erase = 0;
  324.     (*actions[accepts[state]])();
  325.     switch (zzadd_erase) {
  326.         case 1: goto skip;
  327.         case 2: goto more;
  328.     }
  329. }
  330.  
  331. void
  332. zzadvance()
  333. {
  334.     if (zzstream_in) { ZZGETC_STREAM; zzcharfull = 1; ZZINC;}
  335.     if (zzfunc_in) { ZZGETC_FUNC; zzcharfull = 1; ZZINC;}
  336.     if (!(zzstream_in || zzfunc_in)){
  337.         zzerr_in();
  338.     }
  339. }
  340.  
  341. void
  342. #ifdef __STDC__
  343. zzerrstd(char *s)
  344. #else
  345. zzerrstd(s)
  346. char *s;
  347. #endif
  348. {
  349.         fprintf(stderr,
  350.                 "%s near line %d (text was '%s')\n",
  351.                 ((s == NULL) ? "Lexical error" : s),
  352.                 zzline,zzlextext);
  353. }
  354.  
  355. int
  356. zzerr_in()
  357. {
  358.     fprintf(stderr,"No input stream or function\n");
  359.     /* return eof to get out gracefully */
  360.     return -1;
  361. }
  362.